package com.qf.smsplatform.strategy.service.impl;

import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.index.StandardSubmit;
import com.qf.smsplatform.strategy.service.FilterChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 作者：郝国栋
 * 项目名：IntelliJ IDEA
 * 时间:2020/8/26  16:17
 * 描述:路由策略
 */
@Service(value = "router")
@Slf4j
public class RouterStrategy implements FilterChain {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("【路由策略】 路由策略执行了....");

        //1. 发送的消息是submit/

        //2. 发送的队列是 TOPIC_SMS_GATEWAY + submit.getGatewayId();
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY + submit.getGatewayID();
        //3. 创建队列,AmqpAdmin
        Queue queue = QueueBuilder.durable(queueName).build();
        amqpAdmin.declareQueue(queue);

        //4. 发送消息
        rabbitTemplate.convertAndSend(queueName,submit);
        log.info("【路由策略】 消息发送成功!!");
        return true;
    }
}
